home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / media_scan.js < prev    next >
Text File  |  2006-02-07  |  10KB  |  355 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. const MediaLibrary = new Components.Constructor("@songbird.org/Songbird/MediaLibrary;1", "sbIMediaLibrary");
  28.  
  29. var theSongbirdStrings = document.getElementById( "songbird_strings" );
  30.  
  31. var aMediaLibrary = null;
  32. var msDBQuery = null;
  33. var polling_interval = null;
  34.  
  35. var aQueryFileArray = new Array();
  36.  
  37. var theTitle = document.getElementById( "rmp_demo_media_scan_title" );
  38. theTitle.value = "";
  39.  
  40. var theLabel = document.getElementById( "rmp_demo_media_scan_label" );
  41. theLabel.value = "";
  42.  
  43. var theProgress = document.getElementById( "rmp_demo_media_scan" );
  44. theProgress.setAttribute( "mode", "undetermined" );
  45. theProgress.value = 0;
  46.  
  47. var theProgressValue = 0; // to 100;
  48. var theProgressText = "";
  49.  
  50. var aMediaScan = new Object;
  51. var aMediaScanQuery = new Object;
  52.  
  53. // Init the text box to the last url played (shrug).
  54. var polling_interval;
  55.  
  56. function onLoad()
  57. {
  58.   if ( ( typeof( window.arguments[0] ) != 'undefined' ) && ( typeof( window.arguments[0].URL ) != 'undefined' ) )
  59.   {
  60.     try
  61.     {
  62.       aMediaScan = new sbIMediaScan();
  63.       aMediaScanQuery = new sbIMediaScanQuery();
  64.       
  65.       if (aMediaScan && aMediaScanQuery)
  66.       {
  67.         SBDataSetValue( "media_scan.open", true ); // ?  Don't let this go?
  68.       
  69.         aMediaScanQuery.SetDirectory(window.arguments[0].URL);
  70.         aMediaScanQuery.SetRecurse(true);
  71.                 
  72.         aMediaScan.SubmitQuery(aMediaScanQuery);
  73.         
  74.         polling_interval = setInterval( onPollScan, 333 );
  75.         
  76.         var scanning = "Scanning";
  77.         try
  78.         {
  79.           scanning = theSongbirdStrings.getString("media_scan.scanning");
  80.         } catch(e) {}
  81.         theTitle.value = scanning + " -- " + window.arguments[0].URL;
  82.       }
  83.     }
  84.     catch(err)
  85.     {
  86.       alert(err);
  87.     }
  88.   }
  89.   return true;
  90. }
  91.  
  92. function onPollScan()
  93. {
  94.   try
  95.   {
  96.     if ( !aMediaScanQuery.IsScanning() )
  97.     {
  98.       var complete = "Complete";
  99.       try
  100.       {
  101.         complete = theSongbirdStrings.getString("media_scan.complete");
  102.       } catch(e) {}
  103.       theLabel.value = complete;
  104.       clearInterval( polling_interval );
  105.       onScanComplete( aMediaScanQuery );
  106.       document.getElementById("button_ok").removeAttribute( "disabled" );
  107.       document.getElementById("button_ok").focus();
  108.     }   
  109.     else
  110.     {
  111.       var len = aMediaScanQuery.GetFileCount();
  112.       if ( len )
  113.       {
  114.         var value = aMediaScanQuery.GetLastFileFound();
  115.         theLabel.value = ConvertUrlToFolder(value);
  116.       }
  117.     }
  118.   }
  119.   catch ( err )  
  120.   {
  121.     alert( err );
  122.   }
  123. }
  124.  
  125. function onScanComplete( mediaScanQuery )
  126. {
  127.   theProgress.removeAttribute( "mode" );
  128.  
  129.   if ( mediaScanQuery.GetFileCount() )
  130.   {
  131.     try
  132.     {
  133.       msDBQuery = new sbIDatabaseQuery();
  134.       aMediaLibrary = new MediaLibrary();
  135.       aMediaLibrary = aMediaLibrary.QueryInterface( Components.interfaces.sbIMediaLibrary );
  136.             
  137.       if ( ! msDBQuery || ! aMediaLibrary )
  138.       {
  139.         return -1;
  140.       }
  141.       
  142.       msDBQuery.SetAsyncQuery( true );
  143.       msDBQuery.SetDatabaseGUID( "songbird" );
  144.       
  145.       aMediaLibrary.SetQueryObject( msDBQuery );
  146.  
  147.       // Take the file array and for everything that seems to be a media url, add it to the database.
  148.       var i, count, total;
  149.       total = aMediaScanQuery.GetFileCount();
  150.       
  151.       for ( i = 0, count = 0; i < total; i++ )
  152.       {
  153.         var the_url = aMediaScanQuery.GetFilePath( i );
  154.         if ( IsMediaUrl( the_url ) )
  155.         {
  156.           var keys = new Array( "title" );
  157.           var values = new Array();
  158.           
  159.           values.push( ConvertUrlToDisplayName( the_url ) );
  160.           
  161.           aMediaLibrary.AddMedia( the_url, keys.length, keys, values.length, values, false, true );
  162.           aQueryFileArray.push( values[0] );
  163.           
  164.           count++;
  165.         }
  166.       }
  167.       
  168.       if ( count )
  169.       {
  170.         var adding = "Adding";
  171.         try
  172.         {
  173.           adding = theSongbirdStrings.getString("media_scan.adding");
  174.         } catch(e) {}
  175.         theTitle.value = adding + " (" + count + ")";
  176.         theLabel.value = "";
  177.         ret = msDBQuery.Execute();
  178.         polling_interval = setInterval( onPollQuery, 333 );
  179.       }
  180.       else
  181.       {
  182.         var none = "Nothing";
  183.         try
  184.         {
  185.           none = theSongbirdStrings.getString("media_scan.none");
  186.         } catch(e) {}
  187.         theTitle.value = none;
  188.       }
  189.     }
  190.     catch(err)
  191.     {
  192.       alert(err);
  193.     }
  194.   }
  195.   else
  196.   {
  197.     var none = "Nothing";
  198.     try
  199.     {
  200.       none = theSongbirdStrings.getString("media_scan.none");
  201.     } catch(e) {}
  202.     theTitle.value = none;
  203.   }
  204. }
  205.  
  206. var lastpos = 0;
  207. function onPollQuery()
  208. {
  209.   try
  210.   {
  211.     if ( msDBQuery )
  212.     {
  213.       var len = msDBQuery.GetQueryCount();
  214.       var pos = msDBQuery.CurrentQuery() + 1;
  215. //      if ( pos == lastpos )
  216.       {
  217.       }
  218.       lastpos = pos;
  219.       if ( len == pos )
  220.       {
  221.         var added = "Added";
  222.         var complete = "Complete";
  223.         try
  224.         {
  225.           added = theSongbirdStrings.getString("media_scan.added");
  226.           complete = theSongbirdStrings.getString("media_scan.complete");
  227.         } catch(e) {}
  228.         theTitle.value = len + " " + added;
  229.         theLabel.value = complete;
  230.         theProgress.value = 100.0;
  231.         clearInterval( polling_interval );
  232.       }   
  233.       else
  234.       {
  235.         var adding = "Adding";
  236.         try
  237.         {
  238.           adding = theSongbirdStrings.getString("media_scan.adding");
  239.         } catch(e) {}
  240.         theTitle.value = adding + " (" + pos + "/" + len + ")";
  241.         theLabel.value = aQueryFileArray[ pos ];
  242.         theProgress.value = ( pos * 100.0 ) / len;
  243.       }
  244.     }
  245.   }
  246.   catch ( err )
  247.   {
  248.     alert( err );
  249.   }
  250. }
  251.  
  252. function doOK()
  253. {
  254.   SBDataSetValue( "media_scan.open", false ); // ?  Don't let this go?
  255.   document.defaultView.close();
  256.   return true;
  257. }
  258. function doCancel()
  259. {
  260.   SBDataSetValue( "media_scan.open", false ); // ?  Don't let this go?
  261.   document.defaultView.close();
  262.   return true;
  263. }
  264.  
  265. function ConvertUrlToDisplayName( url )
  266. {
  267.   // Set the title display  
  268.   var the_value = "";
  269.   if ( url.lastIndexOf('/') != -1 )
  270.   {
  271.     the_value = url.substring( url.lastIndexOf('/') + 1, url.length );
  272.   }
  273.   else if ( url.lastIndexOf('\\') != -1 )
  274.   {
  275.     the_value = url.substring( url.lastIndexOf('\\') + 1, url.length );
  276.   }
  277.   else
  278.   {
  279.     the_value = url;
  280.   }
  281.   // Convert any %XX to space
  282.   var percent = the_value.indexOf('%');
  283.   if ( percent != -1 )
  284.   {
  285.     var remainder = the_value;
  286.     the_value = "";
  287.     while ( percent != -1 )
  288.     {
  289.       the_value += remainder.substring( 0, percent );
  290.       remainder = remainder.substring( percent + 3, url.length );
  291.       percent = remainder.indexOf('%');
  292.       the_value += " ";
  293.       if ( percent == -1 )
  294.       {
  295.         the_value += remainder;
  296.       }
  297.     }
  298.   }
  299.   if ( ! the_value.length )
  300.   {
  301.     the_value = url;
  302.   }
  303.   return the_value;
  304. }
  305.  
  306. function ConvertUrlToFolder( url )
  307. {
  308.   // Set the title display  
  309.   var the_value = "";
  310.   if ( url.lastIndexOf('/') != -1 )
  311.   {
  312.     the_value = url.substring( 0, url.lastIndexOf('/') );
  313.   }
  314.   else if ( url.lastIndexOf('\\') != -1 )
  315.   {
  316.     the_value = url.substring( 0, url.lastIndexOf('\\') );
  317.   }
  318.   else
  319.   {
  320.     the_value = url;
  321.   }
  322.   return the_value;
  323. }
  324.  
  325. function IsMediaUrl( the_url )
  326. {
  327.   if ( ( the_url.indexOf ) && 
  328.         (
  329.           // Protocols at the beginning
  330.           ( the_url.indexOf( "mms:" ) == 0 ) || 
  331.           ( the_url.indexOf( "rtsp:" ) == 0 ) || 
  332.           // File extensions at the end
  333.           ( the_url.indexOf( ".pls" ) != -1 ) || 
  334.           ( the_url.indexOf( ".m3u" ) == ( the_url.length - 4 ) ) || 
  335. //          ( the_url.indexOf( ".rm" ) == ( the_url.length - 3 ) ) || 
  336. //          ( the_url.indexOf( ".ram" ) == ( the_url.length - 4 ) ) || 
  337. //          ( the_url.indexOf( ".smil" ) == ( the_url.length - 5 ) ) || 
  338.           ( the_url.indexOf( ".mp3" ) == ( the_url.length - 4 ) ) ||
  339.           ( the_url.indexOf( ".m4a" ) == ( the_url.length - 4 ) ) ||
  340.           ( the_url.indexOf( ".ogg" ) == ( the_url.length - 4 ) ) ||
  341.           ( the_url.indexOf( ".wma" ) == ( the_url.length - 4 ) ) ||
  342.           ( the_url.indexOf( ".wmv" ) == ( the_url.length - 4 ) ) ||
  343.           ( the_url.indexOf( ".asx" ) == ( the_url.length - 4 ) ) ||
  344.           ( the_url.indexOf( ".asf" ) == ( the_url.length - 4 ) ) ||
  345.           ( the_url.indexOf( ".avi" ) == ( the_url.length - 4 ) ) ||
  346.           ( the_url.indexOf( ".mov" ) == ( the_url.length - 4 ) ) ||
  347.           ( the_url.indexOf( ".mp4" ) == ( the_url.length - 4 ) )
  348.         )
  349.       )
  350.   {
  351.     return true;
  352.   }
  353.   return false;
  354. }
  355.